package Task20;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import org.apache.log4j.Logger;
 
public class Controller {
	static String str;
	static String[] Str = new String[3];
	//the starting workDirectory
	static File workDirectory = new File("C:\\Documents and Settings");
	static BufferedReader input = new BufferedReader(new InputStreamReader (System.in));
	private static final Logger logger=Logger.getLogger(Cmd.class);

	public static void commandLine(){
		System.out.print(workDirectory.getPath()+" >");
		try{
		//reading the command and divide on the parts
		str=input.readLine();
		//add in logFile the command
		logger.info("Command: "+str);
		Str = str.split(" ");
		}
		catch (IOException e) {
        System.out.print("Error the input");
        logger.error("Error the input");
		}
	}
	public static void cmd(){
			String[] List;
			try {
				Str = Model.delSpaces(Str);
				List = Model.getObject(Str);
				if (List[0]!=null){
					for (String str: List) System.out.println(str);
					logger.info("Operation completed");
				}
				else {
					System.out.println("An error in command syntax or directory/file not found");
					logger.error("An error in command syntax or directory/file not found");
				}
			} catch (SecurityException e) {
				e.printStackTrace();
				System.out.println("Access is denied");
				logger.error("Access is denied",e);	
			} catch (IllegalAccessException e) {
				e.printStackTrace();
				System.out.println("Access is denied");
				logger.error("Access is denied",e);	
			} catch (NoSuchFieldException e) {
				System.out.println("Internal error");
				logger.error("Internal error",e);		
			} catch (NullPointerException e){
				e.printStackTrace();
				System.out.println("An error in command syntax or directory/file not found");
				logger.error("An error in command syntax or directory/file not found",e);		
			}catch (ArrayIndexOutOfBoundsException e){
				e.printStackTrace();
				System.out.println("Files with this name not found");
				logger.error("Files with this name not found",e);		
			}catch (Exception e) {
				e.printStackTrace();
				System.out.println("Unknow error");
				logger.error("Unknow error",e);
			}
	}
	
	public void changeWD(File newWD){
		workDirectory = newWD;
	}
}
